<template>
	<view class="container">
		<!--列表.开始-->
		<view class="group-box">
			<view class="goods-list">
				<!--循环.开始-->
				<navigator 
					v-for="(item,index) in listData" 
					:key="index" 
					:url="'show?id='+item.id"
					class="list-box">
					<view class="img-box">
						<image mode="widthFix" :src="$api.loadFile(item.goodsProduct.imgUrl?item.goodsProduct.imgUrl:item.goodsProduct.shopGoods.imgUrl)"></image>
					</view>
					<view class="txt-box">
						<text class="title">{{item.title}}</text>
						<view class="point">
							<text class="sell">{{item.point}}</text>
							<text class="original">{{item.goodsProduct.sellPrice}}</text>
						</view>
					</view>
				</navigator>
				<!--循环.结束-->
			</view>
			<uni-load-more :status="loadingType" @clickLoadMore="loadData('add')"></uni-load-more>
		</view>
		<!--列表.结束-->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadingType: "more", //加载状态
				totalCount: 0, //总数量
				totalPages: 0, //总页数
				pageSize: 10, //每页显示条数
				pageIndex: 1, //当前页码
				listData: [], //数据列表
			}
		},
		methods: {
			//初始化数据,带下滑刷新和上滑加载
			async loadData(type='add', loading){
				let _this = this;
				//上滑加载更多
				if(type === 'add'){
					if(_this.loadingType === 'nomore'){
						return;
					}
					_this.loadingType = 'loading';
				}
				//下滑刷新
				else{
					_this.pageIndex=1;
					_this.listData=[];
					_this.loadingType="more";
				}
				
				//加载地址列表
				await _this.$api.request({
					url: `/client/shop/convert?pageSize=${_this.pageSize}&pageIndex=${_this.pageIndex}`,
					method: "GET",
					load: true
				}).then(res=>{
					let pageInfo = JSON.parse(res.header["x-pagination"]);
					_this.totalCount = pageInfo.totalCount;
					_this.totalPages = pageInfo.totalPages;
					let listData = res.data;
					_this.listData = _this.listData.concat(listData);
				}).catch((err)=>{
					_this.loadingType="nomore";
					uni.stopPullDownRefresh();
				});
				
				//判断是否还有下一页
				if(_this.pageIndex >= _this.totalPages){
					_this.loadingType="nomore";
				}else{
					_this.pageIndex++;
					_this.loadingType="more";
				}
				//下滑隐藏动画及上滑事件
				if(type === 'refresh'){
					if(loading == 1){
						uni.hideLoading()
					}else{
						uni.stopPullDownRefresh();
					}
				}
			},
		},
		//页面初始化
		onLoad(options){
			this.loadData('add');
		},
		//下滑刷新
		onPullDownRefresh(){
			this.loadData('refresh');
		},
		//上滑加载更多
		onReachBottom(){
			this.loadData('add');
		}
	}
</script>

<style lang="scss">

</style>
